<!--
  -    Copyright (c) 2018-2025, lengleng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: lengleng (wangiegie@gmail.com)
  -->
<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :page="page"
        :data="tableData"
        :table-loading="tableLoading"
        :option="tableOption"
        :search.sync="searchForm"
        @on-load="getList"
        @search-change="searchChange"
        @refresh-change="refreshChange"
        @size-change="sizeChange"
        @current-change="currentChange"
        @row-update="handleUpdate"
        @row-save="handleSave"
        @row-del="rowDel"
      >
        <template slot="menuLeft">
          <el-button
            size="small"
            icon="el-icon-download"
            @click="exportFile()"
          >导出</el-button>
        </template>
        <template
          slot="menu"
          slot-scope="scope"
        >
          <el-button
            v-if="xiexin_cscomplaint_get"
            type="text"
            @click="showDetail(scope.row.id)"
          >
            查看
          </el-button>
          <el-button
            v-if="xiexin_cscomplaint_edit && scope.row.state!=3"
            type="text"
            @click="openContent(scope.row.id)"
          >
            回复
          </el-button>
        </template>
      </avue-crud>
      <el-dialog
        title="投诉建议回复"
        :visible.sync="dialogVisible"
        width="25%"
        :before-close="handleClose"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >
        <el-form
          ref="form"
          :model="form"
        >
          <el-form-item label="回复内容：">
            <el-input
              type="textarea"
              v-model="form.desc"
            ></el-input>
          </el-form-item>
        </el-form>
        <span
          slot="footer"
          class="dialog-footer"
        >
          <el-button
            type="primary"
            @click="dosubmit"
            icon='el-icon-circle-check'
          >确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog
        title="投诉建议查看"
        :visible.sync="dialogVisible2"
        width="25%"
        :before-close="handleClose"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >
        <p>投诉时间：{{formb.createDate | formatterDate}}</p>
        <p>联系方式：{{formb.phone}}</p>
        <p>投诉内容：{{formb.content}}</p>
        <template v-if="formb.replyDate">
          <el-divider></el-divider>
          <p>回复时间：{{formb.replyDate | formatterDate}}</p>
          <p>回复内容：{{formb.replyContent}}</p>
        </template>
        <span
          slot="footer"
          class="dialog-footer"
        >
          <el-button
            v-if="formb.state!='3'"
            type="primary"
            @click="handleClose"
            icon='el-icon-circle-close'
          >关 闭</el-button>
          <el-button
            v-if="formb.state!='3'"
            type="primary"
            @click="openContent('')"
            icon='el-icon-circle-check'
          >回 复</el-button>
          <el-button
            v-if="formb.state=='3'"
            type="primary"
            icon='el-icon-circle-close'
            @click="handleClose"
          >关 闭</el-button>
        </span>
      </el-dialog>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, getObj, addObj, putObj, delObj, getId, postObj } from '@/api/information/complaint/cscomplaint'
import { tableOption } from '@/const/crud/information/complaint/cscomplaint'
import { mapGetters } from 'vuex'
import { ExportFile } from '@/api/common'
export default {
  name: 'cscomplaint',
  data () {
    return {
      dialogVisible: false,
      dialogVisible2: false,
      searchForm: {},
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      form: {
        desc: ''
      },
      formb: {

      },
      tableLoading: false,
      tableOption: tableOption,
      xiexin_cscomplaint_get: false,
      xiexin_cscomplaint_edit: false,
      selectId: ''
    }
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList () {
      return {
        addBtn: this.vaildData(this.permissions.xiexin_cscomplaint_add, false),
        delBtn: this.vaildData(this.permissions.xiexin_cscomplaint_del, false),
        delBtn: this.vaildData(this.permissions.xiexin_cscomplaint_del, false),
        editBtn: this.vaildData(this.permissions.xiexin_cscomplaint_edit, false)
      };
    }
  },
  created () {
    this.xiexin_cscomplaint_get = this.permissions["xiexin_cscomplaint_get"]
    this.xiexin_cscomplaint_edit = this.permissions["xiexin_cscomplaint_edit"]
  },
  filters: {
    formatterDate: (val) => {
      return (val + '').replace("T", ' ')
    }
  },
  methods: {
    exportFile () {

      ExportFile('/information/cscomplaint/outputComplain', this.searchForm);
    },
    showDetail (id) {
      this.selectId = id
      this.dialogVisible2 = true
      getId(id).then(res => {
        this.formb = res.data.data
      })
    },
    openContent (id) {
      this.form = {
        desc: ''
      }
      this.dialogVisible = true
      if (id) {
        this.selectId = id
      }
    },
    dosubmit (id) {
      if (!this.form.desc) {
        this.$message.warning("请输入回复内容")
        return
      }
      this.dialogVisible = false
      postObj({ id: this.selectId, replyContent: this.form.desc }).then(res => {
        this.showDetail(this.selectId)
        this.getList(this.page)
      })
    },
    handleClose () {
      this.dialogVisible = false
      this.dialogVisible2 = false
      this.getList(this.page)
    },
    getList (page, params) {
      this.tableLoading = true
      fetchList(Object.assign({
        current: page.currentPage,
        size: page.pageSize
      }, params, this.searchForm)).then(response => {
        this.tableData = response.data.data.records
        this.page.total = response.data.data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    rowDel: function (row, index) {
      this.$confirm('是否确认删除此条数据？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delObj(row.id)
      }).then(data => {
        this.$message.success('删除成功')
        this.getList(this.page)
      })
    },
    handleUpdate: function (row, index, done, loading) {
      putObj(row).then(data => {
        this.$message.success('修改成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading();
      });
    },
    handleSave: function (row, done, loading) {
      addObj(row).then(data => {
        this.$message.success('添加成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading();
      });
    },
    sizeChange (pageSize) {
      this.page.pageSize = pageSize
    },
    currentChange (current) {
      this.page.currentPage = current
    },
    searchChange (form, done) {
      this.searchForm = form
      this.page.currentPage = 1
      this.getList(this.page, form)
      done()
    },
    refreshChange () {
      this.getList(this.page)
    }
  }
}
</script>
<style lang="scss" scoped>
/deep/textarea {
  width: 100%;
  resize: none;
  height: 100px;
}
</style>